package com.isunland.managesystem.common;

import android.view.View;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ConfigOrignal;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.LocalConfigOriginal;
import com.isunland.managesystem.utils.LocalConifgGenerateUtil;
import com.isunland.managesystem.utils.LogUtil;
import java.util.HashMap;
import org.json.JSONException;

/**
 * Created by wangyanan on 2016/5/4.
 * 动态配置自动完成.在需要配置的界面中调用autoConfig.
 */
public class DynamicConfigLab {
  private static HashMap<String, HashMap<String, LocalConfigOriginal.LocalConfigModel>> sMap;
  private static DynamicConfigLab sDynamicConfigLab;
  private static String sMembercode;
  private String mKey;

  private DynamicConfigLab(BaseVolleyActivity context, String key) {
    mKey = key;
    volleyPostConfig(context);
  }

  public static DynamicConfigLab newInstance(BaseVolleyActivity context, String key) {
    String memberCode = CurrentUser.newInstance(context).getMemberCode();
    if (sMembercode == null//会员id为空
        || (!sMembercode.equals(memberCode))) {//切换账号
      sMembercode = memberCode;
      sMap = null;//切换账号后清空数据,重新加载.
    }

    //初始化条件
    if (sDynamicConfigLab == null//单例为空
        || sMap == null//单例保持内容为空
        || sMap.get(key) == null//模块内容为空
        ) {

      sDynamicConfigLab = new DynamicConfigLab(context, key);
      LogUtil.i("初始化动态配置信息");
    }

    return sDynamicConfigLab;
  }

  public static void clearMap() {
    sMap = null;
  }

  private void volleyPostConfig(BaseVolleyActivity context) {

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_DYNAMIC_CONFIG);
    HashMap<String, String> params = new HashMap<String, String>();
    /**
     *   * @apiParam (参数说明){String}   resourceFlag =subColumns 固定值.
     * @apiParam (参数说明){String}   MemberCode  登陆时获取的memberCode
     * @apiParam (参数说明){String}   dataStatus   = publish 固定值
     */
    params.put("memberCode", sMembercode);
    params.put("resourceFlag", mKey);
    params.put("dataStatus", "publish");
    context.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        LogUtil.i("原始配置=" + arg0);
        //                arg0 = MyUtils.readAssetsFile(MyApplication.getAppContext(), "config.json");
        ConfigOrignal configOriginal = new Gson().fromJson(arg0, ConfigOrignal.class);
        arg0 = configOriginal.getRows().get(0).getContentApp().replace("\\", "");
        LogUtil.i("本地配置=" + arg0);

        LocalConfigOriginal localConfigOriginal =
            new Gson().fromJson(arg0, LocalConfigOriginal.class);

        if (sMap == null) {
          //初始化smap
          sMap = new HashMap<String, HashMap<String, LocalConfigOriginal.LocalConfigModel>>();
        }
        if (!sMap.containsKey(mKey)) {
          //初始化子map

          sMap.put(mKey, new HashMap<String, LocalConfigOriginal.LocalConfigModel>());
          for (LocalConfigOriginal.LocalConfigModel dynamicConfig : localConfigOriginal.getRows()) {
            sMap.get(mKey).put(dynamicConfig.getField(), dynamicConfig);
          }

          //                    for (ConfigOriginal.ConfigModel dynamicConfig : configOriginal.getRows()) {
          //                        if (!sMap.containsKey(dynamicConfig.getModule())){
          //                            HashMap<String, ConfigOriginal.ConfigModel> childMap = new HashMap<String, ConfigOriginal.ConfigModel>();
          //                            sMap.put(dynamicConfig.getModule(),childMap);
          //
          //                        }
          //                        sMap.get(dynamicConfig.getModule()).put(dynamicConfig.getField(), dynamicConfig);
          //                        LogUtil.i("初始化配置信息成功");
          //                    }

          //                    ConfigOriginal.ConfigModel test = sMap.get("deptName");
          //                    LogUtil.i("configSuccess="+test.getTitle());
        }
      }
    });
  }

    /*
     * @param textView 配置标题
     * @param view     配置可见性
     * @param key      唯一标识,通过key来寻找配置.
     */
  //    public void configView(TextView textView, View view, String key) {
  //
  //        try {
  //            ConfigOriginal.ConfigModel config = sMap.get(key);
  //            textView.setText(config.getTitle());
  //            int visible = config.isHidden() ? View.GONE : View.VISIBLE;
  //            view.setVisibility(visible);
  //        } catch (Exception e) {
  //            LogUtil.e("动态配置控件出错", e);
  //        }
  //
  //
  //    }

  public void autoConfigTitle(View view) {
    try {
      HashMap<String, LocalConfigOriginal.LocalConfigModel> childMap = sMap.get(mKey);
      for (String s : childMap.keySet()) {
        String titleTag = mKey + "_" + s + "_" + LocalConifgGenerateUtil.TITLE;
        TextView tvTitle = (TextView) view.findViewWithTag(titleTag);
        if (tvTitle != null) {
          LocalConfigOriginal.LocalConfigModel config = childMap.get(s);
          //设置标题
          tvTitle.setText(config.getTitle());
        }
      }
      LogUtil.i("自动配置标题完成");
    } catch (Exception e) {
      LogUtil.e("自动配置标题出错", e);
    }
  }

  public void autoConfigVisiblity(View view) {
    try {
      HashMap<String, LocalConfigOriginal.LocalConfigModel> childMap = sMap.get(mKey);
      for (String s : childMap.keySet()) {
        String holderTag = mKey + "_" + s + "_" + LocalConifgGenerateUtil.VISIBILITY;
        View holder = view.findViewWithTag(holderTag);
        if (holder != null) {
          LocalConfigOriginal.LocalConfigModel config = childMap.get(s);

          //设置可见性

          int visible = config.isHidden() ? View.GONE : View.VISIBLE;
          holder.setVisibility(visible);
        }
      }
      LogUtil.i("自动配置可见性完成");
    } catch (Exception e) {
      LogUtil.e("自动配置可见性出错", e);
    }
  }
}
